package com.jeeplus.modules.tools.utils;

import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;

/**
 * @version V1.0
 * AES 加密工具类
 * @author 赵钢添加
 */

public class AESUtil
{

    // 生成密钥算法
    private static final String KEY_ALGORITHM = "AES";

    // 加密算法
    private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";

    /**
     * AES 加密操作
     * @param content 待加密内容
     * @param password 加密密码
     * @return 返回Base64转码后的加密数据
     */
    public static String encrypt(String content, String password, String salt) {
        try {
            // 实例化
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            byte[] byteContent = content.getBytes(StandardCharsets.UTF_8);
            // 使用非ECB模式，需要一个向量iv，可增加加密算法的强度
            IvParameterSpec iv = new IvParameterSpec(salt.getBytes());
            // 生成密钥
            SecretKeySpec key = new SecretKeySpec(password.getBytes(), KEY_ALGORITHM);
            // 使用密钥初始化，设置为加密模式
            cipher.init(Cipher.ENCRYPT_MODE, key, iv);
            // 加密
            byte[] result = cipher.doFinal(byteContent);
            // 通过Base64转码返回
            return Base64.encodeBase64String(result);
        } catch (Exception ex) {
            Logger.getLogger(AESUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    /**
     * AES 解密操作
     * @param content 解密内容
     * @param password 密码
     * @return 明文
     */
    public static String decrypt(String content, String password, String salt) {
        try {
            // 实例化
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            // 设置iv
            IvParameterSpec iv = new IvParameterSpec(salt.getBytes());
            // 生成密钥
            SecretKeySpec key = new SecretKeySpec(password.getBytes(), KEY_ALGORITHM);
            // 使用密钥初始化，设置为解密模式
            cipher.init(Cipher.DECRYPT_MODE, key, iv);
            // 解密
            byte[] result = cipher.doFinal(Base64.decodeBase64(content));
            return new String(result, StandardCharsets.UTF_8);
        } catch (Exception ex) {
            Logger.getLogger(AESUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }


    /**
     * 加密算法的测试用例
     * @param args
     */
    public static void main(String[] args) {
        String s = "{\n" +
                "  \"token\": \"1234567abc\",\n" +
                "  \"data_type\": 1,\n" +
                "  \"area_code\": 112345,\n" +
                "  \"enterprise_code\" : \"nm1202\",\n" +
                "  \"enterprise_name\" : \"测试加油站名字\",\n" +
                "  \"date\" : \"2020-1-14 18:46:12\",\n" +
                "  \"object_id\":\"1002\",\n" +
                "  \"fluidic_resistor\" : 1.2,\n" +
                "  \"tank_pressure\": 2.3,\n" +
                "  \"temperature\" : 4.5,\n" +
                "  \"hydrocarbon_pore_volume\" : 5.6,\n" +
                "  \"vapor_emission_concentration\" : 6.7\n" +
                "}";
        String password = "E58828AC2642588BD6B25759DE12ADF8"; // 秘钥
        String salt = "C9796789EAC65B2E"; //盐

        System.out.println("源字符串 s:" + s);
        String s1 = AESUtil.encrypt(s, password, salt);
        System.out.println("加密后字符串 s1:" + s1);

        s1 = "RPnBRBh7j9L3otxYbm3IZqbv3g+0wLa3h7Zx1QSvYlCPPMRD1H8Lifld3vys7k6U";
        String s2 = AESUtil.decrypt(s1, password, salt);
        System.out.println("解密后字符串 s2:" + s2);
    }
}